
'use strict';
define(['app', 'utils/route-config'], function (app, routeConfig) {
	
    /**
	* Accounting services
	*/
	app.factory("categoryServices", function($http) {
		var categories = [];
		return {
			initialize: function(callback, firstAccount) {
				console.log('initialize categoryServices');
				$http.post(app.serviceBase, { "service" : "getCategories", "accountId" : firstAccount.Id}).
				success(function(data, status) 
				{
					console.info("categoryServices.getCategories - call success"); 
					categories = data.response;
					callback(categories);
				})
				.
				error(function(data, status) 
				{
					console.info("categoryServices.getCategories - call failed"); 
					throw status + ' : ' + data;
				});
			},
			all: function(callback) {
				callback(categories);
			},
			add: function(callback, accountId, name, parentId) { 
				parentId = (parentId == undefined) ? -1 : parentId; 
				console.log('categoryServices.addCategory called');
				$http.post(app.serviceBase, { "service" : "addCategory", "accountId": accountId, "name": name, "parentId" : parentId }).
				success(function(data, status) 
				{	
					console.info("categoryServices.addCategory - call success"); 
					categories.push(data.response);
					callback(data);
				})
				.
				error(function(data, status) 
				{
					console.info("categoryServices.addCategory - call failed"); 
					throw status + ' : ' + data;
				});
			},
			deleteCategory: function(callback, category) {
				console.log('categoryServices.deleteCategory called');
				$http.post(app.serviceBase, { "service" : "deleteCategory", "id": category.Id }).
				success(function(data, status) 
				{	
					console.info("categoryServices.deleteCategory - call success"); 
					categories.splice(0, 0, account);
					callback(data);
				})
				.
				error(function(data, status) 
				{
					console.info("categoryServices.deleteCategory - call failed"); 
					throw status + ' : ' + data;
				});
			}
		};
	});

    return app;
});